package com.foster.eric.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.foster.eric.sample.weightloss.form.Measurements;
import com.foster.eric.sample.weightloss.form.Result;
import com.foster.eric.sample.weightloss.service.CalculationService;

/**
 * Returns results from input.
 * 
 * @author ericf
 * 
 */

@Controller
@SessionAttributes("formBean")
public class ResultsController {

	@Autowired
	private CalculationService calculationService; 
	
	@RequestMapping(value = "/results", method = RequestMethod.POST, params="result")
	public String getResults(@ModelAttribute Measurements measurements,
			Model model,BindingResult result) {
		
		List<Result> results = calculationService.calculate(measurements);
		measurements.setResults(results);
		model.addAttribute("formBean", measurements);
		return "home";
	}
	
	@RequestMapping(value = "/results", method = RequestMethod.POST, params="reset")
	public String reset(Model model)
	{
		model.addAttribute("formBean", new Measurements());
		return "home";
	}
}
